<?php

add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );

/**
 * Init plugin options to white list our options
 */
function theme_options_init() {
	register_setting( 'agilsun_options', 'agilsun_theme_options', 'theme_options_validate' );
}

/**
 * Load up the menu page
 */
function theme_options_add_page() {
	add_menu_page( __( 'Theme Options', 'agilsuntheme' ), __( 'Theme Options', 'agilsuntheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}

/**
 * Theme Option Uploader
 */
function mt_admin_scripts() {
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_register_script('my-upload', get_bloginfo('template_url').'/theme-options/uploader.js', array('jquery','media-upload','thickbox'));
	wp_enqueue_script('my-upload');
	wp_enqueue_script('editor');
}

function mt_admin_styles() {
	wp_enqueue_style('thickbox');
}

if (isset($_GET['page']) && $_GET['page'] == 'theme_options') {
	add_action('admin_print_scripts', 'mt_admin_scripts');
	add_action('admin_print_styles', 'mt_admin_styles');
}

function admin_files() {
	?>
<link rel="stylesheet" href="<?php echo get_bloginfo('template_url') . '/theme-options/tabs.css'; ?>" />
<script type="text/javascript" src="<?php echo get_bloginfo('template_url') . '/theme-options/api' ?>/yahoo-dom-event.js"></script>
<script type="text/javascript" src="<?php echo get_bloginfo('template_url') . '/theme-options/api' ?>/element-beta-min.js"></script>
<script type="text/javascript" src="<?php echo get_bloginfo('template_url') . '/theme-options/api' ?>/connection-min.js"></script>
<script type="text/javascript" src="<?php echo get_bloginfo('template_url') . '/theme-options/api' ?>/tabview-min.js"></script>
<?php }

add_action('admin_head', 'admin_files');

/**
 * Create the options page
 */
function theme_options_do_page() {
	global $select_options, $radio_options;

	if ( ! isset( $_REQUEST['settings-updated'] ) )
		$_REQUEST['settings-updated'] = false;

	?>

<div class="wrap">
    <?php screen_icon();?>
    <h2>Theme Options</h2>
    <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
    <div class="updated fade">
        <p><strong>
            <?php _e( 'Options saved', 'agilsuntheme' ); ?>
            </strong></p>
    </div>
    <?php endif; ?>
    <form method="post" action="options.php">
        <?php settings_fields( 'agilsun_options' ); ?>
        <?php $options = get_option( 'agilsun_theme_options' ); ?>
        <div id="content-explorer" style="width: 100%;">
            <ul class="yui-nav" style="width: 100%;">
                <li class="selected"><a href="#">Header</a></li>
                <li><a href="#">Footer</a></li>
            </ul>
            <div class="yui-content">
                <div style="overflow: auto;"><!-- div for content tabs Header -->
                    <div class="metabox-holder" style="width: 100%;">
                        <div class="postbox">
                            <table class="form-table" style="margin:0;">
                                <tr valign="top">
                                    <td style="padding:0;width: 20%;"><h3>Title</h3></td>
                                    <td style="padding:0;width: 40%;"><h3>Value</h3></td>
                                    <td style="padding:0; width: 40%;"><h3>Description</h3></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[imglogo]" style="padding:10px;font-weight:bold;">Logo URL</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><input id="upload_image_logo" type="text" style="width: 100%" name="agilsun_theme_options[imglogo]" value="<?php esc_attr_e( $options['imglogo'] ); ?>" />
                                        <input id="upload_image_button_logo" type="button" class="button" value="Upload Image" style="margin-top: 5px;" /></td>
                                    <td style="margin:5px 0;"><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Logo URL (220 x 62 px).</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[twitterpage]" style="padding:10px;font-weight:bold;">Twitter</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><input type="text" name="agilsun_theme_options[twitterpage]" style="width: 100%" value="<?php esc_attr_e( $options['twitterpage'] ); ?>" /></td>
                                    <td><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Twitter page.</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[facebookpage]" style="padding:10px;font-weight:bold;">Facebook</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><input type="text" name="agilsun_theme_options[facebookpage]" style="width: 100%" value="<?php esc_attr_e( $options['facebookpage'] ); ?>" /></td>
                                    <td><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Facebook fanpage.</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[googlepage]" style="padding:10px;font-weight:bold;">Google+</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><input type="text" name="agilsun_theme_options[googlepage]" style="width: 100%" value="<?php esc_attr_e( $options['googlepage'] ); ?>" /></td>
                                    <td><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Google+ page.</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[skype]" style="padding:10px;font-weight:bold;">Skype</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><input type="text" name="agilsun_theme_options[skype]" style="width: 100%" value="<?php esc_attr_e( $options['skype'] ); ?>" /></td>
                                    <td><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Skype.</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[headerscript]" style="padding:10px;font-weight:bold;">Header Script</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><textarea class="large-text" style="width: 100%" rows="7" name="agilsun_theme_options[headerscript]"><?php echo esc_textarea( $options['headerscript'] ); ?></textarea></td>
                                    <td style="margin:5px 0;"><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Ex: Code Google Analytics.</p></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
                <div><!-- div for content tabs Footer -->
                    <div class="metabox-holder" style="width: 100%;">
                        <div class="postbox">
                            <table class="form-table" style="margin:0;">
                                <tr valign="top">
                                    <td style="padding:0;width:200px;"><h3>Title</h3></td>
                                    <td style="padding:0;width:500px;"><h3>Value</h3></td>
                                    <td style="padding:0;"><h3>Description</h3></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[contactinfo]" style="padding:10px;font-weight:bold;">Contact Info</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><?php wp_editor($options['contactinfo'], 'agilsun_theme_options[contactinfo]'); ?></td>
                                    <td style="margin:5px 0;"><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Footer contact infomation.</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[copyright]" style="padding:10px;font-weight:bold;">Copyright</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><input type="text" name="agilsun_theme_options[copyright]" style="width: 100%" value="<?php esc_attr_e( $options['copyright'] ); ?>" /></td>
                                    <td style="margin:5px 0;"><p style="margin:0;margin-left:10px;font-style:italic;font-size:11px;">Ex: Copyright &copy; 2013.</p></td>
                                </tr>
                                <tr valign="top" style="border-bottom:1px solid #ccc;">
                                    <td style="padding:5px 0;"><label for="agilsun_theme_options[footerscript]" style="padding:10px;font-weight:bold;">Footer Script</label></td>
                                    <td style="padding:5px 0;margin-left:5px;"><textarea class="large-text" style="width: 100%" rows="7" name="agilsun_theme_options[footerscript]"><?php echo esc_textarea( $options['footerscript'] ); ?></textarea></td>
                                    <td style="margin:5px 0;"></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <!-- #yui-content --> 
        </div>
        <!-- #content-explorer --> 
        
        <script type="text/javascript">
			var myTabs = new YAHOO.widget.TabView("content-explorer");
		</script>
        <p style="width:600px ;text-align:left; float:left;">
            <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'mlabtheme' ); ?>" />
        </p>
    </form>
</div>
<?php
}

/**
 * Sanitize and validate input. Accepts an array, return a sanitized array.
 */
function theme_options_validate( $input ) {

	// Say our text option must be safe text with no HTML tags
	$input['skype'] = wp_filter_nohtml_kses( $input['skype'] );

	return $input;
}

?>